package com.usefullc.demo.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.common.enums.ValidActionEnum;
import com.usefullc.system.validator.UsefulValid;
import com.usefullc.demo.domain.Test1;
import com.usefullc.demo.param.Test1Param;
import com.usefullc.demo.vo.Test1Vo;
import com.usefullc.demo.query.Test1Query;
import com.usefullc.demo.service.Test1Service;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.param.IdsParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static com.usefullc.system.controller.WebResultBuilder.successResult;

/**
 * 测试1控制类
 * @author: 星空
 * @date: 20220622
 */
@RestController
@RequestMapping("/shop/test1/")
@Api( tags="测试1")
public class Test1Controller extends BaseController {

    @Autowired
    private Test1Service test1Service;

    @ApiOperationSupport(author = "1")
    @ApiOperation(value = "测试1列表",response = WebResult.class)
    @GetMapping(path = "list",produces = "application/json")
    public WebResult list(@UsefulValid(query = true) Test1Query query) {
        Pager<Test1Vo> page = test1Service.findVoByPage(query);
        return WebResultBuilder.successResult(page);
    }

    @ApiOperationSupport(author = "1")
    @ApiOperation(value = "测试1详情",response = WebResult.class)
    @GetMapping(path = "detail",produces = "application/json")
    public WebResult detail(@ApiParam(value = "示例ID") @Valid @RequestParam Long id) {
        Test1 test1 = test1Service.findById(id);
        return WebResultBuilder.successResult(test1);
    }


    @ApiOperationSupport(author = "1")
    @ApiOperation(value = "创建测试1", response = WebResult.class)
    @PostMapping(path = "/save", produces = "application/json")
    @UsefulValid
    public WebResult save(@UsefulValid(save=true) @RequestBody Test1Param param) {
        Test1 test1 = BeanUtils.beanCopy(param, Test1.class);
        return successResult(test1Service.save(test1));
    }

    @ApiOperationSupport(author = "1")
    @ApiOperation(value = "修改测试1", response = WebResult.class)
    @PostMapping(path = "/update", produces = "application/json")
    public WebResult update(@UsefulValid(update=true) @RequestBody Test1Param param) {
        Test1 oldBean = test1Service.findById(param.getId());
        BeanUtils.beanCopy(param, oldBean);
        return successResult(test1Service.save(oldBean));
    }

    @ApiOperationSupport(author = "1")
    @PostMapping(path = "del",produces = "application/json")
    @ApiOperation(value = "删除测试1",response = WebResult.class)
    public WebResult del(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        test1Service.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "1")
    @PostMapping(path = "delByLogic",produces = "application/json")
    @ApiOperation(value = "逻辑删除测试1",response = WebResult.class)
    public WebResult delByLogic(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        test1Service.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }








}
